﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PaidOvertime
{
    public partial class Ustawienia : Form
    {
        public Ustawienia()
        {
            InitializeComponent();

            DodajUstawieniaALL();
            
          
        }

        private void btnAnuluj_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnZapisz_Click(object sender, EventArgs e)
        {

            Save();

        }

        private bool changed = false;

        private void EditValueChanging(object sender, EventArgs e)
        {
            changed = true;
        }

        private void Save()
        {
            

            foreach (Control c in tableLayoutPanel1.Controls)
            {
                if (c is TextBox && c.Tag != null)
                {
                    Properties.Settings.Default[c.Tag.ToString()] = c.Text;
                }
            }
            Properties.Settings.Default.Save();
            changed = false;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (changed && MessageBox.Show("Zapisac zmiany?", "PaidOvertime", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                Save();
            }
            base.OnClosing(e);

        }

        private void DodajUstawieniaALL()
        {
            int i = 0;
            DodajProperty(i++, "Numer umowy", "XnumerIdenX", "ISD15/486");
            DodajProperty(i++, "Nazwa Firmy", "XnazwaFirmyX", "nazwa1");
            DodajProperty(i++, "Adres Firmy 1", "XadresFirmy1X", "adres1");
            DodajProperty(i++, "Adres Firmy 2", "XadresFirmy2X", "adres2");
            DodajProperty(i++, "NIP", "XnipX", "nip");
            DodajProperty(i++, "Bank", "XbanknX", "mBank");
            DodajProperty(i++, "Numer konta", "XnumerKontaX", "numer konta");

            DodajProperty(i++, "Konktrahent", "XnazwaKontrX", "ISD DESIGN POLAND Sp. z o. o.");
            DodajProperty(i++, "Adres Konktrahenta 1", "XadresKonktr1X", "ul. Bohaterów 10 Sudeckiej Dywizji Piechoty 5");
            DodajProperty(i++, "Adres Konktrahenta 2", "XadresKonktr2X", "35-307 Rzeszów");
            DodajProperty(i++, "NIP konktrahenta", "XnipKontrX", "951-22-91-330");
        }

        private void DodajProperty(int row, string text, string code, string value)
        {
            if (Properties.Settings.Default[code] != null && !string.IsNullOrWhiteSpace(Properties.Settings.Default[code].ToString()))
                value = Properties.Settings.Default[code].ToString();
     

            DodajPropertyGotowe(row, text, code, value);
        }

        private void DodajPropertyGotowe(int row, string text, string code, string value)
        {
            System.Windows.Forms.Label label1 = new Label();
            System.Windows.Forms.TextBox textBox1 = new TextBox();
            label1.Anchor = System.Windows.Forms.AnchorStyles.Right;
            label1.AutoSize = true;
            label1.Location = new System.Drawing.Point(3, 38);
            label1.Name = "label1";
            label1.Size = new System.Drawing.Size(35, 13);
            label1.TabIndex = row*2;
            label1.Text = text;
            // 
            // textBox1
            // 
            textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            textBox1.Location = new System.Drawing.Point(44, 35);
            textBox1.Name = "textBox1";
            textBox1.Size = new System.Drawing.Size(629, 20);
            textBox1.TabIndex = row*2 + 1;
            textBox1.Text = value;
            textBox1.Tag = code;

            this.tableLayoutPanel1.Controls.Add(label1, 0, row);
            this.tableLayoutPanel1.Controls.Add(textBox1, 1, row);
            textBox1.TextChanged += EditValueChanging;
        }
    }
}
